from decimal import Decimal


def create():
    f = open('students.txt', 'a+', encoding='utf-8')
    f.close()


def read():
    students_txt = []
    f = open('students.txt', 'r', encoding='utf-8')
    for x in f:
        student_list = x.strip().split(',')
        name = student_list[0].split('：')
        num = student_list[1].split('：')
        chinese = student_list[2].split('：')
        math = student_list[3].split('：')
        english = student_list[4].split('：')
        student = {'name': name[1], 'num': num[1], 'chinese': Decimal(chinese[1]), 'english': Decimal(english[1]),
                   'math': Decimal(math[1])}
        students_txt.append(student)
    f.close()
    return students_txt


def save():
    f = open('students.txt', 'w', encoding='utf-8')
    for student in students:
        line = f'姓名：{student['name']},学号：{student['num']},语文：{student['chinese']},数学：{student['math']},英语：{student['english']}\n'
        f.write(line)
    f.close()


def same(num):
    for student in students:
        if student['num'] == num:
            return student
    return None


def long():
    if len(students) == 0:
        print('信息为空，无法操作！')
        return 0
    else:
        return 1


def find_student(num):
    for student in students:
        if student['num'] == num:
            return student
    print('未找到该学生！')
    return None


def add_student():
    try:
        name = input("请输入学生姓名:")
        num = input("请输入学号:")
        if same(num):
            print('已有此学号学生！请重新输入！')
        else:
            chinese = Decimal(input("请输入语文成绩:"))
            english = Decimal(input("请输入英语成绩:"))
            math = Decimal(input("请输入数学成绩:"))
            student = {'name': name, 'num': num, 'chinese': chinese, 'english': english, 'math': math}
            students.append(student)
            print('录入完成！')
    except ValueError:
        print('输入有误！')


def del_student():
    try:
        num = input("请输入要删除的学生学号:")
        student = find_student(num)
        if student:
            students.remove(student)
            print('已删除!')
    except ValueError:
        print('输入有误！')


def modify_student():
    try:
        num = input("请输入要修改的学生学号:")
        same(num)
        student = find_student(num)
        if student:
            print("请重新输入成绩！")
            student['chinese'] = Decimal(input("请输入语文成绩:"))
            student['english'] = Decimal(input("请输入英语成绩:"))
            student['math'] = Decimal(input("请输入数学成绩:"))
            print('修改完成！')
    except ValueError:
        print('输入有误！')


def show_student():
    try:
        num = input('请输入需要查询的学生学号，全部查询输入all:')
        if num == 'all':
            print("所有学生成绩信息如下：")
            for student in students:
                print(
                    f"姓名: {student['name']} 学号: {student['num']} 语文成绩: {student['chinese']} 英语成绩: {student['english']} 数学成绩: {student['math']}")
        else:
            student = find_student(num)
            if student:
                score = student['chinese'] + student['english'] + student['math']
                average = score / 3
                print(
                    f"姓名: {student['name']} 学号: {student['num']} 语文成绩: {student['chinese']} 英语成绩: {student['english']} 数学成绩: {student['math']}")
                print(f"总分: {score} 平均分: {average:.2f}")
    except ValueError:
        print('输入有误！')


while True:
    create()
    students = read()
    choice = input("选择你要进行的操作: 1.录入学生成绩 2.删除学生成绩 3.修改学生成绩 4.查询学生成绩 5.退出程序:")
    if choice == "1":
        add_student()
        save()
    elif choice == "2":
        if long():
            del_student()
            save()
    elif choice == "3":
        if long():
            modify_student()
            save()
    elif choice == "4":
        if long():
            show_student()
    elif choice == "5":
        print("已经退出程序！")
        exit()
    else:
        print('请输入1-5之间的数字！')
